package com.luculent.utils;

import java.util.Map.Entry;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

public class SQLUtils {
	public static String toSql(String tableName, JsonArray data){
		StringBuilder sqlList = new StringBuilder();
		for(JsonElement je : data){
			StringBuilder sql = new StringBuilder("INSERT INTO ");
			sql.append(tableName).append("(");
			for(Entry<String, JsonElement> keyvalue : je.getAsJsonObject().entrySet()){
				sql.append(keyvalue.getKey()).append(", ");
			}
			sql.deleteCharAt(sql.lastIndexOf(","));
			sql.append(") ");
			sql.append("values (");
			for(Entry<String, JsonElement> keyvalue : je.getAsJsonObject().entrySet()){
				if(keyvalue.getValue().isJsonNull())
					sql.append('\'').append(keyvalue.getValue().getAsJsonNull().toString()).append("', ");
				else if (keyvalue.getValue().isJsonPrimitive())
					sql.append('\'').append(keyvalue.getValue().getAsString()).append("', ");
			}
			sql.deleteCharAt(sql.lastIndexOf(","));
			sql.append(");");
			
			sqlList.append(sql);
		} 
		return sqlList.toString();
	}
}
